package com.li.client.management;

import java.util.ArrayList;
import java.util.List;

import com.li.client.entities.RoleAdmin;
import com.li.client.entities.SectionName;
import com.li.client.service.DataUtilityManager;
import com.li.client.util.AppStyle;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class ManagementScreenWidget extends TabSet {
	private static final String TITLE_MANAGEMENT_SCREEN = "Management Screen";
	public static final String BORDER_1PX_DDDDDD = "1px solid #A7ABB4";
	
	private VLayout gridLayout;
	private HLayout bodyLayout;
	private TreeGrid usersTreeGrid;
	
	private static final String SECTION_ID_1 = "section_id_1";
	private static final String TABLE_NAME_BORROW = "Borrow";
	private static final String TABLE_NAME_RETURN = "Return";
	private static final String TABLE_NAME_QUERY_LIB = "All book info";
	private static final String TABLE_NAME_ENROLL = "New Book Enroll";
	private static final String TABLE_NAME_BATCH_ENROLL = "Batch Enrollment";
	public static final String[] USER_TABLE_NAMES = { TABLE_NAME_BORROW, TABLE_NAME_RETURN, TABLE_NAME_QUERY_LIB, TABLE_NAME_BATCH_ENROLL };	
	
	private static final String TITLE_OPERATIONS = "Operations";
	private List<String> sectionOnRole;
	
	public ManagementScreenWidget() {
		Tab tab = new Tab(TITLE_MANAGEMENT_SCREEN);
		initSectionName();
		
		initialize();

		tab.setPane(bodyLayout);
		addTab(tab);
		
		setGridInGridLayout(sectionOnRole.get(0));
		
	}
	
	private void initSectionName() {
		sectionOnRole = new ArrayList<String>();
		List<RoleAdmin> roleAdmin = DataUtilityManager.getCurrentUser().getUserRole().getRoleAdmin();
		for (RoleAdmin ra : roleAdmin) {
			sectionOnRole.add(SectionName.getValusAs(ra.getSectionName()).getLabel());
		}
	}

	private void setGridInGridLayout(String elementName) {
		gridLayout.setHeight100();
		gridLayout.setWidth100();
		gridLayout.removeMembers(gridLayout.getMembers());
		
		if (TABLE_NAME_BORROW.equals(elementName)) {
			gridLayout.addMember(new BorrowBookManager());
		} else if (TABLE_NAME_RETURN.equals(elementName)) {
			gridLayout.addMember(new ReturnBookManager());
		} else if (TABLE_NAME_QUERY_LIB.equals(elementName)) {
			gridLayout.addMember(new AllBookInfoManager());
		} else if (TABLE_NAME_ENROLL.equals(elementName)) {
			
		} else if (TABLE_NAME_BATCH_ENROLL.equals(elementName)) {
			gridLayout.addMember(new BatchUpLoadManager());
		}
	}

	private void initialize() {
		if (bodyLayout != null && bodyLayout.getMembers().length > 0)
		{
			bodyLayout.removeMembers(bodyLayout.getMembers());
		}
		bodyLayout = new HLayout();
		bodyLayout.addMember(getNavigationLayout());
		if (gridLayout == null)
		{
			gridLayout = new VLayout();
			gridLayout.setHeight100();
			gridLayout.setWidth100();
			gridLayout.setLayoutLeftMargin(AppStyle.px5);
			gridLayout.setLayoutRightMargin(AppStyle.px5);
			gridLayout.setLayoutBottomMargin(AppStyle.px5);
			gridLayout.setLayoutTopMargin(AppStyle.px5);
			gridLayout.setBorder(BORDER_1PX_DDDDDD);
		}
		bodyLayout.addMember(gridLayout);
	}

	private Canvas getNavigationLayout() {
		final VLayout leftSideLayout = new VLayout();
		leftSideLayout.setWidth(AppStyle.px210);
		leftSideLayout.setShowResizeBar(true);
//		initResourcesTreeGrid(leftSideLayout);

		SectionStack sectionStack = new SectionStack();
		sectionStack.setShowExpandControls(true);
//		sectionStack.setTitle("op");

		SectionStackSection[] sectionsArray;

		SectionStackSection adminSection = new SectionStackSection("Section Admin");
		initAdminTreeGrid();
		adminSection.setID(SECTION_ID_1);
		adminSection.addItem(usersTreeGrid);
		
		SectionStackSection systemDataSection = new SectionStackSection("Section Data");
		sectionsArray = new SectionStackSection[] { adminSection, systemDataSection };
		sectionStack.addSection(adminSection);
		sectionStack.addSection(systemDataSection);
//
//		setSectionEntitlement(sectionTitles, sectionStack, sectionsArray);
		leftSideLayout.addMember(sectionStack);

		return leftSideLayout;
	}
	
	private void initAdminTreeGrid()
	{
		usersTreeGrid = createTreeGrid(sectionOnRole.toArray(new String[sectionOnRole.size()]), TITLE_OPERATIONS);
		
		
		usersTreeGrid.addRecordClickHandler(new RecordClickHandler()
		{
			public void onRecordClick(RecordClickEvent event)
			{
				int rowNo = event.getRecordNum();
				if (rowNo > 0)
				{
					TreeNode selectedNode = usersTreeGrid.getData().getAllNodes()[rowNo];
					String selectedTableName = selectedNode.getAttribute(TableTreeNode.TABLE_NAME_ATTRIBUTE);
					if (selectedTableName != null)
					{
						setGridInGridLayout(selectedTableName);
					}
				}
			}
		});
	}

	private TreeGrid createTreeGrid(String[] nodes, String parentTitle)
	{
		Tree tablesTree = new Tree();
		tablesTree.setModelType(TreeModelType.PARENT);
		tablesTree.setNameProperty(TableTreeNode.TABLE_NAME_ATTRIBUTE);
		tablesTree.setIdField(TableTreeNode.TABLE_ID_ATTRIBUTE);
		tablesTree.setParentIdField(TableTreeNode.PART_OF_ATTRIBUTE);
		tablesTree.setShowRoot(false);
		tablesTree.setData(getTablesGridData(nodes, parentTitle));
		TreeGridField field = new TreeGridField(TableTreeNode.TABLE_NAME_ATTRIBUTE, parentTitle);
		field.setCanSort(false);

		TreeGrid tree = new TreeGrid();
		tree.setWidth100();
		tree.setHeight100();
		tree.setFields(field);
		tree.setData(tablesTree);
		tree.setShowHeader(false);
		tree.getData().openAll();

		return tree;
	}
	
	private TreeNode[] getTablesGridData(String[] nodes, String parentTitle)
	{
		int rootNodeId = 1;
		int count = 1;
		TableTreeNode node;
		TreeNode[] treeNodeArr = new TreeNode[(nodes.length) + 1];
		TableTreeNode root = new TableTreeNode(rootNodeId, rootNodeId - 1, parentTitle);
		treeNodeArr[0] = root;

		for (String tableName : nodes)
		{
			node = new TableTreeNode(count + 1, rootNodeId, tableName);
			treeNodeArr[count] = node;
			count++;
		}
		return treeNodeArr;
	}

	
	public static class TableTreeNode extends TreeNode
	{
		private static final String TABLE_ID_ATTRIBUTE = "TableId";
		private static final String PART_OF_ATTRIBUTE = "PartOf";
		private static final String TABLE_NAME_ATTRIBUTE = "TableName";

		public TableTreeNode(int tableID, int partOf, String nodeName)
		{
			setTableID(tableID);
			setPartOf(partOf);
			setTableName(nodeName);
		}

		private void setTableName(String tableName)
		{
			setAttribute(TABLE_NAME_ATTRIBUTE, tableName);
		}

		private void setPartOf(int partOf)
		{
			setAttribute(PART_OF_ATTRIBUTE, partOf);
		}

		private void setTableID(int tableID)
		{
			setAttribute(TABLE_ID_ATTRIBUTE, tableID);
		}
	}

	public Canvas getLayout()
	{
		return this;
	}
}
